<?php


class ccb {
	
	//ERP编号
	private $ec_no='QINQINBABY_ERP';
	private $ec_name='亲亲宝贝ERP';
	
	// 数字签名对象
	private  $signa = null;
	
	// 数字签名算法
	public $sign_algorithm = "MD5WithRSA";
	
	// 加解密对象
	private   $cipher = null;
	
	// 加解密算法
	private  $enc_dec_algorithm = "DESede";
	
	private static   $timeout = 30;
	
	// 上传URL
	private static    $URL_UPLOAD = "http://127.0.0.1:7001/merchant/UploadFileServlet";
	
	// 下载URL
	private static    $URL_DOWNLOAD = "http://127.0.0.1:7001/merchant/DownloadFileServlet";
	
	// 交易URL
	private     $URL_CHANNEL = "http://qinqinbaby.com':18006/api/erp.aspx";
	
	private $deskey=	'zhVnsE8qdeVY+9oHGnAWFc4VZ7BPKnXl';//DES对称密钥
	private $rsaPubKey='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYChbF+zS0d0gzhmY8ubKiygV1elGX1rkXrnHk
	WPgW5zwcXz4X/B7q8Jhqa4J1h+lsnrT12dHUCrk3C0UFacDI50Fnui8ZrJiIitiKwy5ZUIQ1t1xP
	ICXR9quJYKHfGQL3HyQTzL6fepmWvOWmyA0xn22XYDYXODN06yfo19jGqQIDAQAB';//商城公钥验证
	private $rsaPriKey = '';
//ERP私玥签名
	
	function ccb(){
		$this->rsaPriKey = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDhfouGSqiYnD+LxhTnOZc1YbF42dx4sutwd5iAMXoLcCIXKCDN
4NTgdfwMpb6eVjv6EzQl/xXwsBScPnmUWtPrE+BLQxjrmk8OJXp+HFPoiPIc37Sg
wnS+8ltRqnI8us0Pa3F0X5b7k+cawScuS+Rnf48WdOp8CMAuqYsAo33l5QIDAQAB
AoGAJKvRwPsvQ/JVG0QRbjEdeB71H7Q+eJqQU2//qwlg0a9gu+dYvmKL/Bzz6pKh
11oq0taAqpU7WjMohuhaGVhUEh6kMUKSjCcd8P/aaV5FvR21bot4wY5GBukIQYzN
Tb6NB0ZlqRTAKyR22qESRGgU87K5PPJHVjH1rzEriYbOsFkCQQD7qcS1PDo4NtqO
H1GB9xW1C4SauKN3eNQ8TgC7EifR4QpVk1+MF1RGM+zawCAO+mHnuwUP84G/7Qs6
0AYbmTeXAkEA5WFUq9fS0496yj5emF3MjELrc40K6Uhd1dNqRjWaICX1iNm46ulj
122LYsELlmDT4WbVyiDO7BB04t8KCgad4wJAez7Nza/cFzGN4PIneY1mvQ4R96+a
pmSvE4VjyDmA9MDPzc7piPLX27WMNijGym8GSXUGaqRnE3Xj/P5pL1TeqwJBALYY
VXpyRHkVD1qctsXouOike3xo/L+DZpLIWjbQeMGKwOhaR3WdwFgWK7BDJmrEBZCL
7a6f6wT2b4RnofWhmRcCQHO1HPmz7eD3b1+TDHzGXQU9D4mWJMwO3utORSxnuRSz
mdrFyC8WK+4H52Sq4ydWTQtwThlIvDL7uA1P9/8tbsQ=
-----END RSA PRIVATE KEY-----
EOD;
	}
	/**
	 * 验证
	 * @param string  $data 数据
	 * @param string  $signature 数字签名
	 * @param string  $public_key 商城公钥
	 * @return number
	 */
	function isValid($data, $signature) {
		$public_key=$this->rsaPubKey;
		//$pkeyid = openssl_get_publickey($public_key);
		$ret = openssl_verify($data, $signature, $public_key, OPENSSL_ALGO_MD5);
	
		if ($ret == -1) {
			exit('error');
		}
	
		return $ret;
	}
	
	/**
	 * 数字签名
	 * @param unknown_type $data
	 * @param unknown_type $private_key
	 * @return unknown
	 */
	function sign($data) {
		

		$privkey=$this->rsaPriKey;
		//echo $privkey;
		$pkeyid = openssl_get_privatekey($privkey);
		openssl_sign($data, $signature, $pkeyid, OPENSSL_ALGO_MD5);
		//openssl_sign($data, $signature, openssl_pkey_get_private($privkey),OPENSSL_ALGO_MD5);
		openssl_free_key($pkeyid);
	
		return $signature;
	}
	
	/**
	 * 加密
	 * @param string $data 明文
	 * @param string $key  密钥
	 * @param bool $use3des 3des
	 * @return string
	 */
	function encrypt($data, $key='', $use3des = true) {
		$cipher =  $use3des ? MCRYPT_TRIPLEDES : MCRYPT_DES;
		$modes = MCRYPT_MODE_ECB;
		if(!$key){
			$key= $this->deskey;
		}
		$key = base64_decode($key);
		# Add PKCS7 padding.
		$block = mcrypt_get_block_size($cipher, $modes);
		$pad = $block - (strlen($data) % $block);
		$data .= str_repeat(chr($pad), $pad);
	
		$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher, $modes), MCRYPT_RAND);
		$encrypted = @mcrypt_encrypt($cipher, $key, $data, $modes, $iv);
	
		return $encrypted;
	}
	/**
	 * 解密
	 * @param string $data base编码的密文
	 * @param string $key  密钥
	 * @param bool $use3des 
	 * @return string
	 */
	function decrypt($data, $key='', $use3des = true) {
		if(!$key){
		$key= $this->deskey;
		}
		$cipher = $use3des ? MCRYPT_TRIPLEDES : MCRYPT_DES;
		$modes = MCRYPT_MODE_ECB;
		$key = base64_decode($key);
		$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( $cipher, $modes ), MCRYPT_RAND );
		$data = @mcrypt_decrypt ( $cipher, $key, $data, $modes, $iv ); // Strip
		// padding
		// out.
		$block = mcrypt_get_block_size ( $cipher, $modes );
		$pad = ord ( $data [($len = strlen ( $data )) - 1] );
		$decrypted = substr ( $data, 0, strlen ( $data ) - $pad );
		return $decrypted;
	}

	
	/**
	 * 发送报文
	 *
	 * @param in
	 *            输入xml报文数组
	 * @param merchant_no
	 *            ERP编号
	 * @param type
	 *            channel：ERP渠道请求报文 service：ERP系统请求报文
	 *
	 * @return
	 */
	public   function doComm($post) {

		$xml=$this->ArraytoXML($post);
		$xml = iconv("utf-8", "GBK", $xml);
		//echo $xml;
		log_write($xml,'com');
		//签名
		$signa=$this->sign($xml);
		
		$signa=base64_encode($signa);
		//echo $signa.'<br>';
		//加密
		$xml=$this->encrypt($xml, $this->deskey);
		
		$xml=base64_encode($xml);
		//echo $xml;
		//$xml = "LTGzFi4SR/9qhYPGN62IAqgU+/AtxRKhmtYV7RM33rcoVmC7ZLvA8HB/Em2q83hiHXQwmy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1Z56ghM5wRGxGcGWmrxvvWijBwikBDzEGZTB5cC53q0wTwuzDSiy1CekxzR9Ma0Xpoq5bz8CWH4or/zBAynxu8EP8fAwlCW8skzOSZ3HcYoBNQ0uavLXHAjxsQ9fKTerGG8v22LY1rFP6TCeAvRoPA9pMH2c6O/aZ6THNH0xrRemirlvPwJYfiiv/MEDKfG7wQ/x8DCUJbyVzMNAV+vBmBxxbSoljVtviPGxD18pN6sMw8eaOCndmMtTo64HvRDTZkirobSN8oVKX+FB7jZYgvf6A7eqMocZRKS7s6lAl43vFvbvJFcbFdIrO5T0YhrtG6yvgi8e9cIsHeIuMUj0kTv4vwOcvCgCzjuD8+0Qg9bAf75pu6NXAFzadpPNqil50kGAHt+b0MGYakmdGDMnEfhAxpJ6K94rrpvHGx1sT6GCuN9Ffpgg5ZNtWnso+MTZTXehaUR410RYdpbJalNXADV4Rl5JVkZ7xbU22kURIvlU3vg+FVzeW4FtBwZ2m3TNweBjMaIgnK09RpQ2TQd2Ho9p2nVOw7EyvUaUNk0Hdh6LrdUrYThkd9Im3tCvgt7Gv07NqKbhuSH/Yt58YS4UaI=";
//$signature= "rUCNzZ1qIcZmGwAtbcJUcqSMgZshPOKgRrsjmNvJ0ib/PMnajbDZUobyYtLWfsmki8BkJem2hrz+EN3ZSt7ODpB2urezxuHlG8sV6j8om7pihc/VBd5f/GTceUQKPP4jYI8JSBgrPCDYOYzOAURvgj86uXTkz7qCdVY81xM5q50=";		//post提交
		 $post_string= "xml=".urlencode($xml)."&signature=".urlencode($signa);
		//echo $post_string;
// 		$post_data= array();
// 		$post_data['xml']=urlencode($xml);
// 		$post_data['signature']=urlencode($signa);
		$rst=$this->post($this->URL_CHANNEL, $post_string );
		//$rst=$this->doRequest('POST', $this->URL_CHANNEL, $post_string);
		
		// 处理返回数据
		// $rst=$this->XMLtoArray($rst);
		//echo $rst;
		//echo '---'.substr($rst, 0,10).'</br>';
		// $rtxml=substr($rst, 10,strlen($rst)-10).'<br>';
		  //echo $rst;
		 
		if($rst != false){
		 $resMsg =  $this->decrypt(substr($rst, 138,strlen($rst)-138), $this->deskey);
	 
		// $resMsg=str_replace('gb2312', 'utf-8', $resMsg);
		 //$resMsg = iconv("utf-8", "gb2312", $resMsg);
 		  //echo '[[[[['.$resMsg;
//  		 try {
//  		 	$resMsg =	iconv("gb2312", "utf-8", $resMsg);
//  		 }catch(Exception $e) {
//  		 	$resMsg = mb_convert_encoding($resMsg, 'utf-8','auto');//
//  		 }
		 // echo iconv_get_encoding($resMsg).'--' ;
// 		 if(mb_check_encoding($resMsg,'GBK')){
// 		 	echo 'GBK';
// 		 }
		 if(mb_check_encoding($resMsg,'utf-8')){
 		 	$resMsg = mb_convert_encoding($resMsg, 'utf-8','auto');//
 		 	//echo 'utf-8';
		 }elseif(mb_check_encoding($resMsg,'GBK')){
		 	$resMsg = mb_convert_encoding($resMsg, 'utf-8','GBK');//
		 	//echo 'GBK';
		 }
		 else{
		 	$resMsg =	iconv("GB2312", "utf-8", $resMsg);
		 	//echo 'GB2312';
		 }
 		// echo $resMsg;
 		 $resMsg=str_replace('gb2312', 'utf-8', $resMsg);
 		
		 $resMsg= $this->XMLtoArray($resMsg);
		 log_write($resMsg,'rst'.$resMsg['HEAD']['chanl_trad_no']);
		// var_dump($resMsg);
 		/*if(!$resMsg){
 		 	$resMsg['HEAD']['resp_code'] = '';
 		 	$resMsg['HEAD']['resp_msg']='拆报文失败，可能请求服务异常';
 		 	$resMsg['BODY'] ['FIELD'] = '';
 		 	$resMsg['BODY'] ['FIELD-LIST'] = '';
 		 }*/
 		}else{
			$resMsg['HEAD']['resp_code'] = '';
			$resMsg['HEAD']['resp_msg']='请求有误，可能网络不稳定';
			$resMsg['BODY'] ['FIELD'] = '';
			$resMsg['BODY'] ['FIELD-LIST'] = '';
		 
		}
		return $resMsg;
	}
	
	/**
	 * 解析返回报文
	 */
	function parseRt($str) {
	
	}
	
	
	/**
	 * POST Curl版本
	 *
	 * 使用方法：
	 *
	 * $post_string = "xml=request&signature=sign";
	 *
	 */
	
	function post($remote_server, $post_string) 
	{
		try{
		$ch = curl_init ();		
		$stime=time();
		curl_setopt ( $ch, CURLOPT_URL, $remote_server );
		curl_setopt ( $ch, CURLOPT_POST, 1);
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 15);
		curl_setopt ( $ch, CURLOPT_POSTFIELDS,  $post_string );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		
		//curl_setopt ( $ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta" );
		$data = curl_exec ( $ch );
		// echo '---';
		curl_close ( $ch );
		}catch (Exception $e){
			//echo $e;
			$data=false;
		}
		$etime=time();
		
		if($etime - $stime >= 15){
			//$data='超时';
		}
		return $data;
	
	}
	
	/**
	 * xml报文转为数组
	 * @param unknown_type $arr
	 */
	function XMLtoArray($XML) {
		$xml_parser = xml_parser_create ();
		xml_parse_into_struct ( $xml_parser, $XML, $vals );
		xml_parser_free ( $xml_parser );
		// wyznaczamy tablice z powtarzajacymi sie tagami na tym samym poziomie
		$_tmp = '';
		foreach ( $vals as $xml_elem ) {
			$x_tag = $xml_elem ['tag'];
			$x_level = $xml_elem ['level'];
			$x_type = $xml_elem ['type'];
			if ($x_level != 1 && $x_type == 'close') {
				if (isset ( $multi_key [$x_tag] [$x_level] ))
					$multi_key [$x_tag] [$x_level] = 1;
				else
					$multi_key [$x_tag] [$x_level] = 0;
			}
			if ($x_level != 1 && $x_type == 'complete') {
				if ($_tmp == $x_tag)
					$multi_key [$x_tag] [$x_level] = 1;
				$_tmp = $x_tag;
			}
		}
		// jedziemy po tablicy
		foreach ( $vals as $xml_elem ) {
			$x_tag = $xml_elem ['tag'];
			$x_level = $xml_elem ['level'];
			$x_type = $xml_elem ['type'];
			if ($x_type == 'open')
				$level [$x_level] = $x_tag;
			$start_level = 1;
			$php_stmt = '$xml_array';
			if ($x_type == 'close' && $x_level != 1)
				$multi_key [$x_tag] [$x_level] ++;
			while ( $start_level < $x_level ) {
				$php_stmt .= '[$level[' . $start_level . ']]';
				if (isset ( $multi_key [$level [$start_level]] [$start_level] ) && $multi_key [$level [$start_level]] [$start_level])
					$php_stmt .= '[' . ($multi_key [$level [$start_level]] [$start_level] - 1) . ']';
				$start_level ++;
			}
			$add = '';
			if (isset ( $multi_key [$x_tag] [$x_level] ) && $multi_key [$x_tag] [$x_level] && ($x_type == 'open' || $x_type == 'complete')) {
				if (! isset ( $multi_key2 [$x_tag] [$x_level] ))
					$multi_key2 [$x_tag] [$x_level] = 0;
				else
					$multi_key2 [$x_tag] [$x_level] ++;
				$add = '[' . $multi_key2 [$x_tag] [$x_level] . ']';
			}
			if (isset ( $xml_elem ['value'] ) && trim ( $xml_elem ['value'] ) != '' && ! array_key_exists ( 'attributes', $xml_elem )) {
				if ($x_type == 'open')
					$php_stmt_main = $php_stmt . '[$x_type]' . $add . '[\'content\'] = $xml_elem[\'value\'];';
				else
					$php_stmt_main = $php_stmt . '[$x_tag]' . $add . ' = $xml_elem[\'value\'];';
				eval ( $php_stmt_main );
			}
			if (array_key_exists ( 'attributes', $xml_elem )) {
				if (isset ( $xml_elem ['value'] )) {
					$php_stmt_main = $php_stmt . '[$x_tag]' . $add . '[\'content\'] = $xml_elem[\'value\'];';
					eval ( $php_stmt_main );
				}
				foreach ( $xml_elem ['attributes'] as $key => $value ) {
					$php_stmt_att = $php_stmt . '[$x_tag]' . $add . '[$key] = $value;';
					eval ( $php_stmt_att );
				}
			}
		}
		
		// 重新组织xml
		$head = array ();
		foreach ( $xml_array ['MESSAGE'] ['HEAD'] ['FIELD'] as $k => $v ) {
			if(isset($v ['content'])){
				$head [$v ['NAME']] = $v ['content'];
			}
		}
		
		$body = array ();
		if(isset($xml_array ['MESSAGE'] ['BODY']['FIELD'])){
			foreach ( $xml_array ['MESSAGE'] ['BODY'] ['FIELD'] as $k => $v ) {
				if(isset($v ['content'])){
					$body [$v ['NAME']] = $v ['content'];
				}
			}
		}
		
		//var_dump($xml_array['MESSAGE']['BODY'] );
		// echo $xml_array ['MESSAGE'] ['BODY'] ['FIELD-LIST']['NAME'];
		$list = array ();
		if (isset ( $xml_array ['MESSAGE'] ['BODY'] ['FIELD-LIST'] )) {
			$filed = array();
			if (! isset ( $xml_array ['MESSAGE'] ['BODY'] ['FIELD-LIST'] ['FIELD-LIST'] ['FIELD'] )) {
				foreach ( $xml_array ['MESSAGE'] ['BODY'] ['FIELD-LIST'] ['FIELD-LIST'] as $k => $v ) {
					
					foreach ( $v ['FIELD'] as $fk => $fv ) {
						$list [$v ['NAME']] [$fv ['NAME']] = $fv ['content'];
					}
				
				}
			} else {
				foreach ( $xml_array ['MESSAGE'] ['BODY'] ['FIELD-LIST'] ['FIELD-LIST'] ['FIELD'] as $fk => $fv ) {
					$list [0] [$fv ['NAME']] = $fv ['content'];
				}
			
			}
		}
		
		$xml_array ['MESSAGE'] ['HEAD'] = $head;
		$xml_array ['MESSAGE'] ['BODY'] ['FIELD'] = $body;
		$xml_array ['MESSAGE'] ['BODY'] ['FIELD-LIST'] = $list;
		return $xml_array['MESSAGE'];
	}    // END XMLtoArray
	
	/**
	 * 数组转为xml报文
	 * @param array $data
	 */
	public function  ArraytoXML($data){
		$field = '';
		$field_list = '';
		
		isset($data['head']['resp_code']) or $data['head']['resp_code']='';
		isset($data['head']['resp_msg']) or $data['head']['resp_msg']='SUCCESS';
		
		if(isset($data['field'] )){
		foreach ($data['field'] as $k=>$v){
			$field.='<field name="'.$k.'"><![CDATA['.$v.']]></field>';
		}
		}
		if(isset($data['field-list'] )){
			if(count($data['field-list'])){
			$field_list = '<field-list name="ARRAY_'.$data['head']['chanl_trad_no'].'">';
			$i=0;
			foreach ($data['field-list'] as $k=>$v){
				$field_list .='<field-list name="'.$i.'">';
				foreach ($v as $sk=>$sv){
					$field_list.='<field name="'.$sk.'"><![CDATA['.$sv.']]></field>';
				}
				$field_list .='</field-list>';
				$i++;
			}
			$field_list .='</field-list>';
			}
		}
		
		$tot_rec = 0;
		$page = 1;
		if(isset($data['head']['tot_rec'])) $tot_rec = $data['head']['tot_rec'];
		if(isset($data['head']['page'])) $page = $data['head']['page'];
			
		$xml='<?xml version="1.0" encoding="gb2312" ?>
				<message>
					<head>
							<field name="version"><![CDATA[100]]></field>
							<field name="type"><![CDATA['.$data['head']['type'].']]></field>
							<field name="chanl_no"><![CDATA[30]]></field>
							<field name="chanl_sub_no"><![CDATA[3001]]></field>
							<field name="chanl_date"><![CDATA['.date('Ymd').']]></field>
							<field name="chanl_time"><![CDATA['.date('His').']]></field>							 
							<field name="chanl_flow_no"><![CDATA['.date('YmdHis').']]></field>						 
							<field name="chanl_trad_no"><![CDATA['.$data['head']['chanl_trad_no'].']]></field>
							<field name="term_inf"><![CDATA[15.12.6.49]]></field>
							<field name="resp_code"><![CDATA['.$data['head']['resp_code'].']]></field>
							<field name="resp_msg"><![CDATA['.$data['head']['resp_msg'].']]></field>';
		
		$xml .=	'<field name="page"><![CDATA['.$page.']]></field>';
		$xml .='<field name="maxrow"><![CDATA[20]]></field>';
		if(isset($data['head']['locstr'])) $xml .='<field name="locstr"><![CDATA['.$data['head']['locstr'].']]></field>';
		if($tot_rec)$xml .='<field name="tot_rec"><![CDATA['.$tot_rec.']]></field>';
		
		$xml .='</head>';
		$xml .='<body>'.$field.$field_list;
		$xml .='</body>';
		$xml .='</message>';
		
		return $xml;
	}
}
 
?>